<template>
    <view class="page" style="overflow: hidden;">
        <view class="content" @touchstart="hideDrawer">
            <scroll-view
                    class="msg-list"
                    scroll-y="true"
                    :scroll-with-animation="scrollAnimation"
                    :scroll-into-view="scrollToView"
                    @scrolltoupper="loadHistory"
                    :upper-threshold="30"
            >
                <!-- 加载历史数据waitingUI -->
                <view class="loading" v-if="isHistoryLoading">
                    <view class="spinner">
                        <view class="rect1"></view>
                        <view class="rect2"></view>
                        <view class="rect3"></view>
                        <view class="rect4"></view>
                        <view class="rect5"></view>
                    </view>
                </view>

                <uni-load-more status="noMore" v-if="noMore && !isHistoryLoading"/>


                <view class="row" v-for="(row, index) in msgList.list" :key="index" :id="'msg' + row.msg.id">
                    <!-- 系统消息 -->
                    <system-bubble :row="row"></system-bubble>

                    <block v-if="row.type == 0 && (index == 0 || row.msg.time - msgList.list[index - 1].msg.time > 300)">
                        <view class="system" style="margin-bottom: 10upx;">
                            <!-- 文字消息 -->
                            <view class="text" style="background: #F4F5F6;color: #8f8f94;">{{
                                timestampFormat(row.msg.time + '') }}
                            </view>
                        </view>
                    </block>

                    <!-- 用户消息 -->
                    <block v-if="row.type == 0">

                        <left-bubble :msgImgList="msgImgList" :row="row"
                                     :playMsgId="playMsgid" :index="index" :myData="user_info" :chat_id="chat_id"
                                     @openRedEnvelopeFunc="openRedEnvelope" @copyFunc="copyMessage"
                                     @goDetails="goDetails"
                                     @rollBackFunc="rollBackMsg"></left-bubble>

                        <right-bubble :msgImgList="msgImgList" :row="row"
                                      :playMsgId="playMsgid" :index="index" :myData="user_info" :chat_id="chat_id"
                                      @openRedEnvelopeFunc="openRedEnvelope" @copyFunc="copyMessage"
                                      @goDetails="goDetails"
                                      @rollBackFunc="rollBackMsg"></right-bubble>

                    </block>

                </view>
            </scroll-view>
        </view>

        <!-- 抽屉栏 -->
        <im-drawer :popupLayerClass="popupLayerClass" :hideEmoji="hideEmoji" :emojiList="emojiList" :hideMore="hideMore"
                   @addEmoji="addEmoji" @sendRedEnvelopes="sendRedEnvelopes" @sendMsg="sendMsg"></im-drawer>

        <!-- 底部输入框 -->
        <footer-input @textMsgFunc="textMsgFunc" @switchVoice="switchVoice" @chooseEmoji="chooseEmoji"
                      @sendMsg="sendMsg" @sendText="sendText"
                      @showMore="showMore" @textareaFocus="textareaFocus" @hideDrawer="hideDrawer"
                      @openDrawer="openDrawer"
                      :disabledSay="msgList.is_msg" :textMsg2="textMsg" :popupLayerClass="popupLayerClass"
                      :isVoice="isVoice"></footer-input>

        <red-envelope @robRed="robRed" @closeRedEnvelope="closeRedEnvelope" :windowsState="windowsState"
                      :redenvelopeData="redenvelopeData" @toDetails="toRedDetails"></red-envelope>

    </view>
</template>
<script>
    import uniLoadMore from '@dcloudio/uni-ui/lib/uni-load-more/uni-load-more.vue';
    import FooterInput from '@/components/chat/footer-input.vue';
    import RedEnvelope from '@/components/chat/red-envelope.vue';
    import SystemBubble from '@/components/chat/system-bubble.vue';
    import LeftBubble from '@/components/chat/left-bubble.vue';
    import RightBubble from '@/components/chat/right-bubble.vue';
    import ImDrawer from '@/components/chat/im-drawer.vue'
    import emoj_data from '../../static/js/message/emoji_data.js';
    import websocket from '../../util/websocket';
    import _hook from '../../util/_hook';
    import _action from '../../util/_action';
    import _data from '../../util/_data';
    import uniCopy from '../../util/uni-copy.js';
    import api from '../../util/api.js';
    //import VueClipboard from 'vue-clipboard2'

    export default {
        components: {
            uniLoadMore,
            FooterInput,
            RedEnvelope,
            SystemBubble,
            RightBubble,
            LeftBubble,
            ImDrawer
        },
        data() {
            return {
                rClickId: '',
                lClickId: '',
                //文字消息
                textMsg: '',
                //消息列表
                isHistoryLoading: false,
                scrollAnimation: false,
                scrollToView: '',
                msgList: {
                    show_name: '',
                    list: [],
                    type: 0,
                    is_msg: 0,
                    is_action: 0
                },
                msgImgList: [],
                noMore: 0,
                isVoice: false,
                playMsgid: null,
                // 抽屉参数
                popupLayerClass: '',
                // more参数
                hideMore: true,
                //表情定义
                hideEmoji: true,
                emojiList: emoj_data.emoji_list,
                //表情图片图床名称 ，由于我上传的第三方图床名称会有改变，所以有此数据来做对应，您实际应用中应该不需要
                //onlineEmoji: emoj_data.online_emoji,
                //红包相关参数
                windowsState: '',
                redenvelopeData: {
                    id: null, //红包ID
                    from: null,
                    face: null,
                    blessing: null,
                    money: null,
                    surplus_money: 0,
                },
                chat_id: 0,
                user_info: {},
                AUDIO: null
            };
        },
        onLoad(option) {
            this.chat_id = option.chat_id;
            this.AUDIO = uni.createInnerAudioContext()
            //语音自然播放结束
            this.AUDIO.onEnded((res) => {
            });
        },
        onUnload() {
            /** 暂停语音播放 */
            this.AUDIO.pause();
            /** 这里只保留最新的20条会话记录，以保证初次加载性能 */
            let chat_data = this.msgList;
            chat_data.list = chat_data.list.slice(-20);
            _data.localData(this.chat_id, chat_data);
            /** 去除当前会话的chat_id */
            _data.localData('message_chat_id', '');
            /** 移除监听事件 */
            uni.$off('data_chat_data_unshift');
            uni.$off('data_chat_data_push');
            uni.$off('data_chat_data');
            uni.$off('data_user_info');
            uni.$off('data_chat_data_delete');
        },
        onShow() {
            _hook.routeSonHook();
            /** 先移除监听事件（避免重复触发消息） */
            uni.$off('data_chat_data_unshift');
            uni.$off('data_chat_data_push');
            uni.$off('data_chat_data');
            uni.$off('data_user_info');
            uni.$off('data_chat_data_delete');

            let _this = this, chat_data = _data.localData(_this.chat_id);
            _this.user_info = _data.data('user_info');

            if (chat_data && chat_data.list.length) {
                _this.scrollAnimation = false;
                uni.setNavigationBarTitle({
                    title: chat_data.show_name
                });
                _this.addMsgImgList(chat_data.list);
                chat_data.list = _this.msgDataHandle(chat_data.list);
                _this.msgList = chat_data;
                /** 滚动到指定位置 */
                _this.$nextTick(function () {
                    _this.scrollToView = 'msg' + chat_data.list[chat_data.list.length - 1].msg.id;
                });
                /** 如果是群聊要实时检测群有无禁言 */
                if (chat_data.type == 1) {
                    api.groupState({chat_id: _this.chat_id}).then(res=>{
                      _this.msgList.is_msg = res.data.is_msg;
                      _this.msgList.is_action = res.data.is_action;
                    }).catch(error=>{
                      uni.toast('获取群状态失败');
                    });
                }
            } else {
                websocket.getChatData({
                    data: {
                        chat_id: _this.chat_id,
                        time: 0,
                        is_up: 1
                    },
                    is_action_data: 1
                });
            }

            /** 添加当前的会话chat_id */
            _data.localData('message_chat_id', this.chat_id);

            /** 监听会话数据变化,附加数据到历史消息前面 */
            uni.$on('data_chat_data_unshift', function (data) {
                data = _this.msgDataHandle(data);
                _this.scrollAnimation = false;
                let position_id = _this.msgList.list[0].msg.id;
                _this.msgList.list.unshift(...data);
                /** 滚动到指定位置 */
                _this.$nextTick(function () {
                    _this.scrollToView = 'msg' + position_id;
                });
            });
            /** 监听会话数据变化 */
            uni.$on('data_chat_data_push', function (data) {
                /** 保持页面15条数据，提升性能 */
                _this.noMore = 0;
                data = _this.msgDataHandle(data);
                _this.scrollAnimation = true;
                _this.msgList.list = data;
                /** 滚动到指定位置 */
                _this.$nextTick(function () {
                    _this.scrollToView = 'msg' + data[data.length - 1].msg.id;
                });
            });
            /** 监听撤回消息 */
            uni.$on('data_chat_data_delete', function (list) {
                _this.msgList.list = list;
            });
            /** 监听会话数据变化 */
            uni.$on('data_chat_data', function (data) {
                uni.setNavigationBarTitle({
                    title: data.show_name
                });
                data.list = _this.msgDataHandle(data.list);
                _this.scrollAnimation = false;
                _this.msgList = data;
                /** 滚动到指定位置 */
                _this.$nextTick(function () {
                    _this.scrollToView = 'msg' + data.list[data.list.length - 1].msg.id;
                });
            });
            /** 监听新的个人数据 */
            uni.$on('data_user_info', function (data) {
                _this.user_info = data;
            });
        },
        computed: {
            myPhoto() {
                if (_data.data('cache').local_photo) {
                    return _data.data('cache').local_photo;
                } else {
                    return _data.staticPhotoUrl(_data.data('user_info').photo) ;
                }
            },
            imgRan() {
                return Math.random();
            },
            staticPath() {
                return _data.staticChatUrl(this.chat_id + '/') ;
            },
            staticPhoto() {
                return _data.staticPhoto();
            }
        },
        methods: {
            textMsgFunc(t) {
                this.textMsg = t;
            },
            goPath(path) {
                if (path) {
                    uni.navigateTo({
                        url: path
                    });
                }
            },
            msgDataHandle(data, type) {
                for (let i = 0, j = data.length, msg_img; i < j; i++) {
                    if (data[i].type == 0 && data[i].msg.type == 2) {
                        msg_img = this.staticPath + data[i].msg.content.url.replace('_thumb.', '.');
                        if (type) {
                            this.msgImgList.unshift(msg_img);
                        } else {
                            this.msgImgList.push(msg_img);
                        }
                        data[i].msg.content = this.setPicSize(data[i].msg.content);
                    } else if (data[i].type == 0 && data[i].msg.type == 0) {
                        data[i].msg.content.text = this.replaceEmoji(data[i].msg.content.text);
                        // 匹配聊天记录中的链接,并替换
                        let url = data[i].msg.content.text.match(/((((https?|ftp?):(?:\/\/)?)(?:[-;:&=\+\$]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\?\+=&;:%!\/@.\w_]*)#?(?:[-\+=&;%!\?\/@.\w_]*))?)/ig)
                        if (url && !/\d{3}.gif/.test(url[0])) {
                            data[i].msg.content.url = url[0];
                        }
                    }
                }
                return data;
            },
            addMsgImgList(data) {
                for (let i = 0, j = data.length, msg_img; i < j; i++) {
                    if (data[i].type == 0 && data[i].msg.type == 2) {
                        this.msgImgList.push(this.staticPath + data[i].msg.content.url.replace('_thumb.', '.'));
                    }
                }
            },
            timestampFormat(time) {
                return _action.timestampFormat(time);
            },
            copyMessage(row) {
                let content = row.msg.content;
                let _this = this;
                content.text = content.text.replace(/<(?!img).*?>/g, "")  // 先去掉除图片外的html标签
                let copystr = content.text.replace(/<img [^>]*src=['"][^'"]+(\d{3}.gif)[^>]*>/g, function (match, capture) {
                    for (let i = 0; i < _this.emojiList.length; i++) {
                        let row = _this.emojiList[i];
                        for (let j = 0; j < row.length; j++) {
                            let EM = row[j];
                            if (EM.url == capture) {
                                return (EM.alt);
                            }
                        }
                    }
                });
                const result = uniCopy(copystr)
                if (result === false) {
                    uni.showToast({
                        title: '您的浏览器不支持,请更换浏览器！',
                    })
                } else {
                    uni.showToast({
                        title: '复制成功',
                        icon: 'none'
                    })
                }
            },
            rollBackMsg(row) {
                let _this = this;
                _this.$httpSend({
                    path: '/im/message/withdraw',
                    data: {chat_id: _this.chat_id, type: _this.msgList.type, msg_id: row.msg.id},
                    success_action: true,
                    success(res) {
                        uni.showToast({
                            title: res.msg,
                            icon: 'none',
                            duration: 1500
                        });
                    }
                });
            },
            openLink(row) {
                if (!row.msg.content.url) {
                    return;
                }
                uni.showModal({
                    title: '提示',
                    content: '是否打开链接：' + content.url,
                    success: function (res) {
                        if (res.confirm) {
                            // #ifdef H5
                            location.href = content.url;
                            // #endif
                            // #ifdef APP-PLUS
                            plus.runtime.openURL(content.url);
                            // #endif
                        }
                    }
                });
            },
            goDetails(user_id) {
                /** 如果是群聊，没有权限的话，查看不了其他会员信息 */
                if (this.msgList.type == 1 && this.msgList.is_action == 0) {
                    uni.showToast({
                        title: '没有权限查看',
                        icon: 'none',
                        duration: 1000
                    });
                    return;
                }
                uni.navigateTo({
                    url: '../friend/detail?user_id=' + user_id
                });
            },
            // 接受消息(筛选处理)
            screenMsg(msg) {
                //从长连接处转发给这个方法，进行筛选处理
                if (msg.type == 1) {
                    // 系统消息
                    switch (msg.msg.type) {
                        case 0:
                            this.addSystemTextMsg(msg);
                            break;
                        case 5:
                            this.addSystemRedEnvelopeMsg(msg);
                            break;
                    }
                } else if (msg.type == 0) {
                    // 用户消息
                    switch (msg.msg.type) {
                        case 0:
                            this.addTextMsg(msg);
                            break;
                        case 1:
                            this.addVoiceMsg(msg);
                            break;
                        case 2:
                            this.addImgMsg(msg);
                            break;
                        case 5:
                            this.addRedEnvelopeMsg(msg);
                            break;
                    }
                    //console.log('用户消息');
                    //非自己的消息震动
                    if (msg.msg.user_info.uid != this.user_info.id) {
                        //console.log('振动');
                        uni.vibrateLong();
                    }
                }
                this.$nextTick(function () {
                    // 滚动到底
                    this.scrollToView = 'msg' + msg.msg.id;
                });
            },
            //触发滑动到顶部(加载历史信息记录)
            loadHistory(e) {
                if (this.isHistoryLoading || this.noMore) {
                    return;
                }
                let _this = this;
                this.isHistoryLoading = true; //参数作为进入请求标识，防止重复请求

                websocket.getChatData({
                    send_data: {
                        chat_id: _this.chat_id,
                        time: _this.msgList.list[0].msg.time,
                        is_up: 0
                    },
                    callback(data) {
                        if (data.list.length < 15) {
                            _this.noMore = 1;
                        }
                        _this.isHistoryLoading = false;
                    },
                    is_action_data: 0
                });
            },
            //处理图片尺寸，如果不处理宽高，新进入页面加载图片时候会闪
            setPicSize(content) {
                // 让图片最长边等于设置的最大长度，短边等比例缩小，图片控件真实改变，区别于aspectFit方式。
                let maxW = uni.upx2px(350); //350是定义消息图片最大宽度
                let maxH = uni.upx2px(350); //350是定义消息图片最大高度
                if (content.w > maxW || content.h > maxH) {
                    let scale = content.w / content.h;
                    content.w = scale > 1 ? maxW : maxH * scale;
                    content.h = scale > 1 ? maxW / scale : maxH;
                    let url_array = content.url.split('.');
                    if (content.url.indexOf('_thumb.') == -1) {
                        content.url = url_array[0] + '_thumb.' + url_array[1];
                    }
                }
                return content;
            },
            //更多功能(点击+弹出)
            showMore() {
                this.isVoice = false;
                this.hideEmoji = true;
                if (this.hideMore) {
                    this.hideMore = false;
                    this.openDrawer();
                } else {
                    this.hideDrawer();
                }
            },
            // 打开抽屉
            openDrawer() {
                this.popupLayerClass = 'showLayer';
            },
            // 隐藏抽屉
            hideDrawer() {
                this.popupLayerClass = '';
                setTimeout(() => {
                    this.hideMore = true;
                    this.hideEmoji = true;
                }, 150);
            },

            //发红包
            sendRedEnvelopes() {
                uni.navigateTo({
                    url: 'message/red_envelope?chat_id=' + this.chat_id
                });
                this.hideDrawer();
            },
            //选照片 or 拍照
            getImage(type) {
                this.hideDrawer();
                uni.chooseImage({
                    sourceType: [type],
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                    success: res => {
                        for (let i = 0; i < res.tempFilePaths.length; i++) {
                            uni.getImageInfo({
                                src: res.tempFilePaths[i],
                                success: image => {
                                    //console.log(image.width);
                                    //console.log(image.height);
                                    let msg = {url: res.tempFilePaths[i], w: image.width, h: image.height};
                                    this.sendMsg(msg, 2);
                                }
                            });
                        }
                    }
                });
            },
            // 选择表情
            chooseEmoji() {
                this.hideMore = true;
                if (this.hideEmoji) {
                    this.hideEmoji = false;
                    this.openDrawer();
                } else {
                    this.hideDrawer();
                }
            },
            //添加表情
            addEmoji(em, del) {
                //判断删除按钮
                if (del) {
                    var str;
                    var msglen = this.textMsg.length - 1;
                    let start = this.textMsg.lastIndexOf("[");
                    let end = this.textMsg.lastIndexOf("]");
                    let len = end - start;
                    if (end != -1 && end === msglen && len >= 2 && len <= 4) {
                        // 表情字符
                        str = this.textMsg.slice(0, start);
                    } else {
                        // 普通键盘输入汉字 或者字符
                        str = this.textMsg.slice(0, msglen);
                    }
                    this.textMsg = str
                    return;
                }
                this.textMsg += em.alt;
                this.hideDrawer();
            },
            //获取焦点，如果不是选表情ing,则关闭抽屉
            textareaFocus() {
                let _this = this;
                _this.hideDrawer();
                return;
                if (_this.popupLayerClass == 'showLayer' && this.hideMore == false) {
                    _this.hideDrawer();
                }
            },
            // 发送文字消息
            sendText(text) {
                //this.textMsg = text;
                this.hideDrawer(); //隐藏抽屉
                if (!this.textMsg) {
                    return;
                }
                this.sendMsg('', 0);
                //清空输入框
                this.textMsg = '';
            },
            //替换表情符号为图片
            replaceEmoji(str) {
                let replacedStr = str.replace(/\[([^(\]|\[)]*)\]/g, (item, index) => {
                    // console.log("item: " + item);
                    for (let i = 0; i < this.emojiList.length; i++) {
                        let row = this.emojiList[i];
                        for (let j = 0; j < row.length; j++) {
                            let EM = row[j];
                            if (EM.alt == item) {
                                //在线表情路径，图文混排必须使用网络路径，请上传一份表情到你的服务器后再替换此路径
                                //比如你上传服务器后，你的100.gif路径为https://www.xxx.com/emoji/100.gif 则替换onlinePath填写为https://www.xxx.com/emoji/
                                let imgstr = '<img style="width:22px;height:22px;" src="' + _data.staticUrl() + '/static/emoji/' + EM.url + '">';
                                // console.log("imgstr: " + imgstr);
                                return imgstr;
                            }
                        }
                    }
                });
                // display: flex;
                replacedStr = replacedStr.replace(/\r\n/g, '<br/>');
                replacedStr = replacedStr.replace(/\n/g, '<br/>');
                return '<div selectable="true" style="align-items: center;word-break:break-all;">' + replacedStr + '</div>';
            },
            // 发送消息
            sendMsg(content, type) {
                this.hideDrawer(); //隐藏抽屉
                uni.showLoading();
                let _this = this,
                    sendMsg = content;
                (callback => {
                    switch (type) {
                        /** 文字/表情消息 */
                        case 0:
                            let msg = this.textMsg.replace(/</g, '&lt;');
                            sendMsg = {text: msg};
                            callback();
                            break;
                        /** 语音/图片/视频/文件 */
                        case 1:
                        case 2:
                        case 3:
                        case 4:
                            _this.$httpSendFile({
                                type: 0,
                                local_url: sendMsg.url,
                                data: {
                                    chat_id: _this.chat_id
                                },
                                success(data) {
                                    sendMsg.url = data.save_name;
                                    callback();
                                }
                            });
                            break;
                        /** 红包消息 */
                        case 5:
                            break;
                        default:
                            break;
                    }
                })(() => {
                    _this.$httpSend({
                        path: '/im/message/textMsg',
                        data: {
                            chat_id: _this.chat_id,
                            content_type: type,
                            content: JSON.stringify(sendMsg)
                        },
                        success_action: true,
                        success(res) {
                            uni.hideLoading();
                            console.log(res);
                            switch (res.err) {
                                case 0:
                                    if (type == 1) {
                                        _action.playVoice('/static/voice/voice.mp3');
                                    }
                                    break;
                                case 1:
                                    uni.showModal({
                                        title: '好友提示',
                                        confirmText: '发送好友申请',
                                        content: res.msg,
                                        success: function (res) {
                                            if (res.confirm) {
                                                uni.navigateTo({
                                                    url: '../friend/apply?user_id=' + _data.localData(_this.chat_id).obj_id + '&is_type=0'
                                                });
                                            }
                                        }
                                    });
                                    break;
                                case 2:
                                    uni.showModal({
                                        content: res.msg
                                    });
                                    let data = _data.localData(data.chat_id);
                                    data.is_msg = 1;
                                    _this.msgList.is_msg = 1;
                                    _data.localData(data.chat_id, data);
                                    break;
                                case 3:
                                    uni.showToast(
                                        {title: res.msg, icon: 'none'}
                                    );
                                    /*
                                    uni.redirectTo({
                                        url: '/onmessage/message?chat_id=' + data.chat_id
                                    });
                                    */
                                    break;
                                default:
                                    break;
                            }
                        }
                    });
                });
            },
            // 添加文字消息到列表
            addTextMsg(msg) {
                this.msgList.list.push(msg);
            },
            // 添加语音消息到列表
            addVoiceMsg(msg) {
                this.msgList.list.push(msg);
            },
            // 添加图片消息到列表
            addImgMsg(msg) {
                this.msgImgList.push(this.staticPath + msg.msg.content.url);
                msg.msg.content = this.setPicSize(msg.msg.content);
                this.msgList.list.push(msg);
            },
            addRedEnvelopeMsg(msg) {
                this.msgList.push(msg);
            },
            // 添加系统文字消息到列表
            addSystemTextMsg(msg) {
                this.msgList.list.push(msg);
            },
            // 添加系统红包消息到列表
            addSystemRedEnvelopeMsg(msg) {
                this.msgList.list.push(msg);
            },
            // 打开红包
            openRedEnvelope(index) {
                let msg = this.msgList.list[index].msg;
                if (msg.content.surplus_money == 0 || (('grab_money' in msg.content) && (msg.content.grab_money > 0))) {
                    this.toRedDetails(msg.content)
                    return false;
                }
                this.redenvelopeData = {
                    id: msg.content.id,
                    from: msg.user_info.name,
                    face: _data.staticPhotoUrl(msg.user_info.face),
                    blessing: msg.content.blessing,
                    money: msg.content.money,
                    surplus_money: msg.content.surplus_money,
                    number: msg.content.number,
                    grab_money: 0,
                    msg:''
                };
                this.getRedEnvelopeInfo(index);
            },
            getRedEnvelopeInfo(index) {
                let _this = this;
                uni.showLoading();
                _this.$httpSend(
                    {
                        path: '/im/get/redEnvelopeInfo',
                        data: {
                            id: _this.redenvelopeData.id
                        },
                        success: (data) => {
                            uni.hideLoading();
                            let msg = _this.msgList.list[index].msg;
                            if (msg.content.surplus_money != data.surplus_money) {
                                console.log('update red');
                                //红包状态发生变化
                                msg.surplus_money = data.surplus_money;
                                //_this.$set(_this.msgList,'list',_this.msgList.list)
                                let chat_data = _data.localData(_this.chat_id);
                                for(let i in chat_data.list){
                                    if(chat_data.list[i].msg.id == msg.id){
                                        chat_data.list[i].msg = msg;
                                        console.log(chat_data.list[i]);
                                        _data.localData(_this.chat_id,chat_data);
                                        break;
                                    }
                                }
                                //_this.msgList.list[index]=msg_data;
                            }
                            if (data.grab_money > 0) {
                                _this.toRedDetails(_this.redenvelopeData)
                                return false;
                            }
                            _this.redenvelopeData.surplus_money = data.surplus_money;
                            _this.redenvelopeData.grab_money = data.grab_money;
                            _this.windowsState = 'show';
                        },
                        error(data) {
                            uni.hideLoading();
                            uni.showModal({
                                content: data.msg,
                            });
                        }
                    }
                );
            },
            //抢红包
            robRed() {
                let _this = this;
                _this.$httpSend(
                    {
                        path: '/im/get/grabRedEnvelope',
                        data: {
                            id: _this.redenvelopeData.id
                        },
                        success: (data) => {
                            //_this.redenvelopeData.grab_money = data.money;
                            //_this.$set(redenvelopeData, 'grab_money', data.money)
                            _this.closeRedEnvelope();
                            _this.toRedDetails(_this.redenvelopeData)
                        },
                        error: (data) => {
                            _this.redenvelopeData.grab_money=data.msg;
                            _this.$set(_this.redenvelopeData,'msg',data.msg);
                            _this.$set(_this.redenvelopeData,'surplus_money',1);
                            //_this.closeRedEnvelope();
                            uni.showModal({
                                content: data.msg,
                            });
                        }
                    }
                );
            },
            // 关闭红包弹窗
            closeRedEnvelope() {
                this.windowsState = 'hide';
                setTimeout(() => {
                    this.windowsState = '';
                }, 200);
            },
            //领取详情
            toRedDetails(redenvelopeData) {
                uni.navigateTo({
                    url: 'message/red_details?id=' + redenvelopeData.id
                });
            },
            sendSystemMsg(content, type) {
                let lastid = this.msgList.list[this.msgList.list.length - 1].msg.id;
                lastid++;
                let row = {type: 'system', msg: {id: lastid, type: type, content: content}};
                this.screenMsg(row);
            },
            // 预览图片
            showPic(msg) {
                uni.previewImage({
                    indicator: 'none',
                    current: this.staticPath + msg.content.url.replace('_thumb', ''),
                    urls: this.msgImgList
                });
            },
            // 播放语音
            playVoice(msg) {
                this.AUDIO.stop();
                if (this.playMsgid == msg.id) {
                    this.playMsgid = null;
                } else {
                    this.playMsgid = msg.id;
                    //	this.AUDIO.obeyMuteSwitch = false;
                    this.AUDIO.src = this.staticPath + msg.content.url + '?_=' + Math.random();
                    this.$nextTick(function () {
                        this.AUDIO.play();
                    });
                }
            },
            // 切换语音/文字输入
            switchVoice() {
                this.hideDrawer();
                this.isVoice = this.isVoice ? false : true;
            },
            discard() {
                return false;
            }
        },
        watch: {},
        onNavigationBarButtonTap(e) {
            uni.navigateTo({
                url: './message/more?chat_id=' + this.chat_id + '&type=' + this.msgList.type
            });
        }
    };
</script>

<style lang="scss">
    @import '@/static/css/chat/style.scss';

    .tool_text {
        margin-top: 120 upx;
        font-size: 20 upx;

        margin-left: 0 upx;
        position: absolute;

        top: 0;
        color: #353535;
    }

    .go-url {
        position: fixed;
        z-index: 999;
        right: 103px;
        width: 220px;
        float: left;
        text-align: center;

        .go-url-btn {
            margin-top: 20 upx;
            line-height: 1.2;
            float: inherit;
            margin-left: 18px;
        }

        image {
            width: 100 upx;
            height: 100 upx;
        }
    }
</style>
